home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / javax / swing / OverlayLayout.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  8.3 KB  |  265 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)OverlayLayout.java    1.17 98/08/28
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package javax.swing;
  15.  
  16.  
  17. import java.awt.*;
  18. import java.io.Serializable;
  19.  
  20. /**
  21.  * A layout manager to arrange components over the top
  22.  * of each other.  The requested size of the container
  23.  * will be the largest requested size of the children, 
  24.  * taking alignment needs into consideration.  
  25.  *
  26.  * The alignment is based upon what is needed to properly
  27.  * fit the children in the allocation area.  The children
  28.  * will be placed such that their alignment points are all
  29.  * on top of each other.
  30.  * <p>
  31.  * <strong>Warning:</strong>
  32.  * Serialized objects of this class will not be compatible with 
  33.  * future Swing releases.  The current serialization support is appropriate
  34.  * for short term storage or RMI between applications running the same
  35.  * version of Swing.  A future release of Swing will provide support for
  36.  * long term persistence.
  37.  *
  38.  * @version 1.17 08/28/98
  39.  * @author   Timothy Prinzing
  40.  */
  41. public class OverlayLayout implements LayoutManager2,Serializable {
  42.  
  43.     /**
  44.      * Constructs a layout manager that performs overlay
  45.      * arrangment of the children.  The layout manager
  46.      * created is dedicated to the given container.
  47.      *
  48.      * @param target  The container to do layout against.
  49.      */
  50.     public OverlayLayout(Container target) {
  51.     this.target = target;
  52.     }
  53.  
  54.     /**
  55.      * Indicates a child has changed its layout related information,
  56.      * which causes any cached calculations to be flushed.
  57.      *
  58.      * @param target the container
  59.      */
  60.     public void invalidateLayout(Container target) {
  61.     checkContainer(target);
  62.     xChildren = null;
  63.     yChildren = null;
  64.     xTotal = null;
  65.     yTotal = null;
  66.     }
  67.  
  68.     /**
  69.      * Adds the specified component to the layout. Not used by this class.
  70.      *
  71.      * @param name the name of the component
  72.      * @param comp the the component to be added
  73.      */
  74.     public void addLayoutComponent(String name, Component comp) {
  75.     }
  76.  
  77.     /**
  78.      * Removes the specified component from the layout. Not used by
  79.      * this class.  
  80.      *
  81.      * @param comp the component to remove
  82.      */
  83.     public void removeLayoutComponent(Component comp) {
  84.     }
  85.  
  86.     /**
  87.      * Adds the specified component to the layout, using the specified
  88.      * constraint object.
  89.      *
  90.      * @param comp the component to be added
  91.      * @param constraints  where/how the component is added to the layout.
  92.      */
  93.     public void addLayoutComponent(Component comp, Object constraints) {
  94.     }
  95.  
  96.     /**
  97.      * Returns the preferred dimensions for this layout given the components
  98.      * in the specified target container.  Recomputes the layout if it
  99.      * has been invalidated.  Factors in the current inset setting returned
  100.      * by getInsets().
  101.      *
  102.      * @param target the component which needs to be laid out
  103.      * @return a Dimension object containing the preferred dimensions
  104.      * @see #minimumLayoutSize
  105.      */
  106.     public Dimension preferredLayoutSize(Container target) {
  107.     checkContainer(target);
  108.     checkRequests();
  109.  
  110.     Dimension size = new Dimension(xTotal.preferred, yTotal.preferred);
  111.     Insets insets = target.getInsets();
  112.     size.width += insets.left + insets.right;
  113.     size.height += insets.top + insets.bottom;
  114.     return size;
  115.     }
  116.  
  117.     /**
  118.      * Returns the minimum dimensions needed to lay out the components
  119.      * contained in the specified target container.  Recomputes the layout
  120.      * if it has been invalidated, and factors in the current inset setting.
  121.      *
  122.      * @param target the component which needs to be laid out 
  123.      * @return a Dimension object containing the minimum dimensions
  124.      * @see #preferredLayoutSize
  125.      */
  126.     public Dimension minimumLayoutSize(Container target) {
  127.     checkContainer(target);
  128.     checkRequests();
  129.  
  130.     Dimension size = new Dimension(xTotal.minimum, yTotal.minimum);
  131.     Insets insets = target.getInsets();
  132.     size.width += insets.left + insets.right;
  133.     size.height += insets.top + insets.bottom;
  134.     return size;
  135.     }
  136.  
  137.     /**
  138.      * Returns the minimum dimensions needed to lay out the components
  139.      * contained in the specified target container.  Recomputes the
  140.      * layout if it has been invalidated, and factors in the inset setting
  141.      * returned by getInset().
  142.      *
  143.      * @param target the component which needs to be laid out 
  144.      * @return a Dimension object containing the maximum dimensions
  145.      * @see #preferredLayoutSize
  146.      */
  147.     public Dimension maximumLayoutSize(Container target) {
  148.     checkContainer(target);
  149.     checkRequests();
  150.  
  151.     Dimension size = new Dimension(xTotal.maximum, yTotal.maximum);
  152.     Insets insets = target.getInsets();
  153.     size.width += insets.left + insets.right;
  154.     size.height += insets.top + insets.bottom;
  155.     return size;
  156.     }
  157.  
  158.     /**
  159.      * Returns the alignment along the x axis for the container.
  160.      * If the major axis of the box is the x axis the default
  161.      * alignment will be returned, otherwise the alignment needed
  162.      * to place the children along the x axis will be returned.
  163.      *
  164.      * @param target the container
  165.      * @return the alignment >= 0.0f && <= 1.0f
  166.      */
  167.     public float getLayoutAlignmentX(Container target) {
  168.     checkContainer(target);
  169.     checkRequests();
  170.     return xTotal.alignment;
  171.     }
  172.  
  173.     /**
  174.      * Returns the alignment along the y axis for the container.
  175.      * If the major axis of the box is the y axis the default
  176.      * alignment will be returned, otherwise the alignment needed
  177.      * to place the children along the y axis will be returned.
  178.      *
  179.      * @param target the container
  180.      * @return the alignment >= 0.0f && <= 1.0f
  181.      */
  182.     public float getLayoutAlignmentY(Container target) {
  183.     checkContainer(target);
  184.     checkRequests();
  185.     return yTotal.alignment;
  186.     }
  187.  
  188.     /**
  189.      * Called by the AWT when the specified container needs to be laid out.
  190.      *
  191.      * @param target  the container to lay out
  192.      *
  193.      * @exception AWTError  if the target isn't the container specified to the
  194.      *                      BoxLayout constructor
  195.      */
  196.     public void layoutContainer(Container target) {
  197.     checkContainer(target);
  198.     checkRequests();
  199.     
  200.     int nChildren = target.getComponentCount();
  201.     int[] xOffsets = new int[nChildren];
  202.     int[] xSpans = new int[nChildren];
  203.     int[] yOffsets = new int[nChildren];
  204.     int[] ySpans = new int[nChildren];
  205.  
  206.     // determine the child placements
  207.     Dimension alloc = target.getSize();
  208.     Insets in = target.getInsets();
  209.     alloc.width -= in.left + in.right;
  210.     alloc.height -= in.top + in.bottom;
  211.     SizeRequirements.calculateAlignedPositions(alloc.width, xTotal, 
  212.                            xChildren, xOffsets,
  213.                            xSpans);
  214.     SizeRequirements.calculateAlignedPositions(alloc.height, yTotal,
  215.                            yChildren, yOffsets,
  216.                            ySpans);
  217.  
  218.     // flush changes to the container
  219.     for (int i = 0; i < nChildren; i++) {
  220.         Component c = target.getComponent(i);
  221.         c.setBounds(in.left + xOffsets[i], in.top + yOffsets[i],
  222.             xSpans[i], ySpans[i]);
  223.     }
  224.     }
  225.  
  226.     void checkContainer(Container target) {
  227.     if (this.target != target) {
  228.         throw new AWTError("OverlayLayout can't be shared");
  229.     }
  230.     }
  231.     
  232.     void checkRequests() {
  233.     if (xChildren == null || yChildren == null) {
  234.         // The requests have been invalidated... recalculate
  235.         // the request information.
  236.         int n = target.getComponentCount();
  237.         xChildren = new SizeRequirements[n];
  238.         yChildren = new SizeRequirements[n];
  239.         for (int i = 0; i < n; i++) {
  240.         Component c = target.getComponent(i);
  241.         Dimension min = c.getMinimumSize();
  242.         Dimension typ = c.getPreferredSize();
  243.         Dimension max = c.getMaximumSize();
  244.         xChildren[i] = new SizeRequirements(min.width, typ.width, 
  245.                             max.width, 
  246.                             c.getAlignmentX());
  247.         yChildren[i] = new SizeRequirements(min.height, typ.height, 
  248.                             max.height, 
  249.                             c.getAlignmentY());
  250.         }
  251.         
  252.         xTotal = SizeRequirements.getAlignedSizeRequirements(xChildren);
  253.         yTotal = SizeRequirements.getAlignedSizeRequirements(yChildren);
  254.     }
  255.     }
  256.         
  257.     private Container target;
  258.     private SizeRequirements[] xChildren;
  259.     private SizeRequirements[] yChildren;
  260.     private SizeRequirements xTotal;
  261.     private SizeRequirements yTotal;
  262.     
  263. }
  264.  
  265.